package com.jing.yao.modules.role.controller;

import com.jing.yao.bean.ResponseBean;
import com.jing.yao.component.controller.YaoBaseController;
import com.jing.yao.component.service.YaoBaseService;
import com.jing.yao.holder.UserContentHolder;
import com.jing.yao.modules.role.controller.dto.RoleDTO;
import com.jing.yao.modules.role.controller.param.RoleParam;
import com.jing.yao.modules.role.dao.entity.RoleEntity;
import com.jing.yao.modules.role.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 角色controller
 *
 * @author lirui
 * @since 2020-06-13
 */
@RestController
@RequestMapping("/role")
public class RoleController extends YaoBaseController<RoleParam, RoleEntity, RoleDTO> {

    @Autowired
    private RoleService roleService;

    @Override
    public YaoBaseService<RoleParam, RoleEntity> getService() {
        return roleService;
    }

    @Override
    public RoleEntity getEntity() {
        return new RoleEntity();
    }

    @Override
    public RoleDTO getDTO() {
        return new RoleDTO();
    }

    /**
     * 给角色分配资源
     *
     * @param roleDTO
     * @return
     */
    @RequestMapping("/role/resources")
    public ResponseBean roleResources(@RequestBody RoleDTO roleDTO) {
        String username = UserContentHolder.getContext().getUsername();
        roleDTO.setCreateBy(username);
        roleService.saveRoleResource(roleDTO.getRoleCode(), roleDTO.getResCodes());
        return successBean();
    }
}
